<template>
  <div class="container">
    <div class="sidebar">
      <h2>马嘉璐-公共部分</h2>
      <el-menu default-active="books">
        <el-menu-item index="books" @click="navigateTo('booklist')">图书</el-menu-item>
        <el-menu-item index="authors" @click="navigateTo('authors')">作者</el-menu-item>
        <el-menu-item index="publishers" @click="navigateTo('publishers')">出版社</el-menu-item>
      </el-menu>
    </div>
    <div class="main-content">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    navigateTo(path) {
      const targetPath = `/dashboard/${path}`;
      // 检查是否当前路由已经是目标路由
      if (this.$route.path!== targetPath) {
        this.$router.push({ path: targetPath }).catch(err => {
          // 只处理 NavigationDuplicated 错误，不做其他处理
          if (err.name!== 'NavigationDuplicated') {
            console.error(err);
          }
        });
      }
    }
  }
};
</script>

<style scoped>
.container {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 200px;
  padding: 20px;
  background-color: #f5f5f5;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
  border-radius: 0 10px 10px 0;
}

.main-content {
  flex-grow: 1;
  padding: 20px;
  background-color: #ffffff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  margin-left: 20px;
}
</style>